<template>
  <div>
    <nav-bar>
      <template v-slot:default>个人中心</template>
    </nav-bar>

    <!-- 个人信息区域 -->
    <div class="user-box">
      <div class="user-info">
        <div class="info">
          <img src="~assets/images/touxian.jpg" />
          <div class="user-desc">
            <span>昵称：{{ user.name }}</span>
            <span>登录名：{{ user.email}}</span>
            <span class="name">个性签名：机会只留给有准备的人</span>
          </div>
        </div>
      </div>

      <!-- 消息滚动区域 -->
      <van-notice-bar left-icon="volume-o" :scrollable="false">
        <van-swipe
          vertical
          class="notice-swipe"
          :autoplay="2000"
          :show-indicators="false"
        >
          <van-swipe-item>明月直入，无心可猜。</van-swipe-item>
          <van-swipe-item>仙人抚我顶，结发受长生。</van-swipe-item>
          <van-swipe-item>今人不见古时月，今月曾经照古人。</van-swipe-item>
        </van-swipe>
      </van-notice-bar>


      <!-- 功能区域 -->
      <ul class="user-list">
        <li class="van-hairline--bottom" @click="goTo('/collect')">
          <span>我的收藏</span>
          <van-icon name="arrow" />
        </li>
        <li class="van-hairline--bottom" @click="goTo('/order')">
          <span>我的订单</span>
          <van-icon name="arrow" />
        </li>
        <li class="van-hairline--bottom" @click="goTo('/setting')">
          <span>账号管理</span>
          <van-icon name="arrow" />
        </li>
        <li class="van-hairline--bottom" @click="goTo('/address')">
          <span>地址管理</span>
          <van-icon name="arrow" />
        </li>
        <li class="van-hairline--bottom" @click="goTo('/about')">
          <span>关于我们</span>
          <van-icon name="arrow" />
        </li>
      </ul>
    </div>

    <!-- 底部区域 -->
    <div style="margin: 16px">
      <van-button round block color="#44BA80" @click="logOut">退出登录</van-button>
    </div>
  </div>
</template>

<script>
import navBar from 'components/common/navbar/NavBar.vue'
import { logout, getUser } from 'network/user.js'
import { Toast, Dialog } from 'vant'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { onMounted, reactive, toRefs } from 'vue'

export default {
  name: 'Profile',
  components:{
    navBar
  },
  setup() {
    const router = useRouter()
    const store = useStore()
    const state = reactive({
      user: {}
    })

    onMounted(async () => {
      // 发起请求，获取用户数据
      const data = await getUser()
      // console.log(data);
      state.user = data
    })

    // 退出操作
    const logOut = async () => {

      const confirm = await Dialog.confirm({
        title: '个人中心',
        theme: 'round-button',
        message:
          '确认退出登录吗？',
      })
      if ( confirm === 'confirm') {
        const data = await logout()
        if(data.status === 204) {
          Toast.success('退出成功！')
          // 清除本地存储的 token
          window.localStorage.removeItem('token')
          // 在 vuex 临时存储是否登录 isLogin
          store.commit('setIsLogin', false)
          setTimeout(()=>{
            router.push('/login')
          }, 500)
        }
      }

      
    }

    //  跳转方法
    const goTo = (path, query) => {
      router.push({path, query: query || {}})
    }

    return {
      ...toRefs(state),
      logOut,
      goTo
    }
  }
}
</script>

<style lang="scss" scoped>
.van-notice-bar {
  margin-top: 10px;
}
.notice-swipe {
  height: 40px;
  line-height: 40px;
}

#box {
  background: #fcfcfc;
  height: 100vh;
}
.user-box {
  margin-top: 60px;
  .user-header {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10000;
    width: 100%;
    height: 44px;
    line-height: 44px;
    padding: 0 10px;
    color: #252525;
    background: #fff;
    border-bottom: 1px solid #dcdcdc;
    .user-name {
      font-size: 14px;
    }
  }
  .user-info {
    width: 94%;
    margin: 10px;
    height: 115px;
    background: linear-gradient(90deg, #31c7a7, #a1c7c7);
    box-shadow: 0 2px 5px #269090;
    border-radius: 6px;
    margin-top: 50px;
    text-align: left;
    .info {
      position: relative;
      display: flex;
      width: 100%;
      height: 100%;
      padding: 25px 20px;

      img {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin-top: 4px;
      }
      .user-desc {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        line-height: 20px;
        font-size: 14px;
        color: #fff;
        span {
          color: #fff;
          font-size: 14px;
          padding: 2px 0;
        }
      }
      .account-setting {
        position: absolute;
        top: 10px;
        right: 20px;
        font-size: 13px;
        color: #fff;
        .van-icon-setting-o {
          font-size: 16px;
          vertical-align: -3px;
          margin-right: 4px;
        }
      }
    }
  }
  .user-list {
    padding: 0 8px;
    margin-top: -10px;
    li {
      padding-left: 5px;
      padding-right: 5px;
      height: 40px;
      line-height: 40px;
      display: flex;
      justify-content: space-between;
      font-size: 14px;
      .van-icon-arrow {
        margin-top: 13px;
      }
      margin: 20px 0 !important;
      background: #ffffff;
      border-radius: 3px;
    }
  }
}
</style>
